package cn.shierblog.spider_zhiping;

import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;
import org.openqa.selenium.By;
import org.openqa.selenium.Proxy;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.chrome.ChromeOptions;
import org.openqa.selenium.remote.CapabilityType;
import org.openqa.selenium.remote.DesiredCapabilities;

import java.io.File;
import java.io.FileOutputStream;
import java.io.FileWriter;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Paths;
import java.util.concurrent.LinkedBlockingQueue;
import java.util.concurrent.TimeUnit;

public class BossSpider extends AbstractSpider{

    static LinkedBlockingQueue<Job> jobs = new LinkedBlockingQueue<>();

    public BossSpider(String url) {
        super(url);
    }

    public static void main(String[] args) {

        System.setProperty("webdriver.chrome.driver", "C:\\Program Files (x86)\\Google\\Chrome\\Application\\chromedriver.exe");
        new BossSpider("https://www.zhipin.com/").craw();

    }

      void getNext(ChromeDriver driver, int pageNow) {
        //

        String pageSource = driver.getPageSource();
        String path = (BossSpider.class.getResource("/").getPath() + (pageNow) + "t.html").substring(1);
        File file = new File(path);
        if (!Files.exists(Paths.get(path))) {
            try {
                Files.createFile(Paths.get(path));
            } catch (IOException e) {
                e.printStackTrace();
            }
        }

        try (FileOutputStream fileOutputStream = new FileOutputStream(file)) {
            fileOutputStream.write(pageSource.getBytes());
            Document parse = Jsoup.parse(pageSource);
            Element element = parse.getElementsByClass("job-list").get(0);
            Elements elementsByClass = element.getElementsByClass("info-primary");
            elementsByClass.forEach(element1 -> {
                Job job = new Job();
                Element element2 = element1.getElementsByClass("job-name").get(0);
                String jobTitle = element2.text();
                String jobArea = element1.getElementsByClass("job-area-wrapper").text();
                Element element3 = element1.getElementsByClass("job-limit").get(0);
                String salary = element3.child(1).text();
                String edu = element3.child(2).text();
                Element companyText = element1.getElementsByClass("company-text").get(0);
                job.setName(companyText.text());
                job.setJob(jobTitle);
                job.setCity(jobArea);
                job.setEdu(edu);
                job.setSalary(salary);
                jobs.add(job);
            });
            WebElement next = driver.findElement(By.className("next"));
            if (next != null) {
                driver.executeScript("arguments[0].click();", next);
                Thread.sleep(5);
                getNext(driver, pageNow + 1);
            }
        } catch (Exception e) {

        }


    }

    @Override
    public void spider()  throws Exception{
        WebElement input = driver.findElement(By.cssSelector("#wrap > div.column-search-panel > div > div > div.search-form > form > div.search-form-con > p > input"));
        WebElement submit = driver.findElement(By.cssSelector("#wrap > div.column-search-panel > div > div > div.search-form > form > button"));
        input.clear();
        input.sendKeys("供应链");
        Thread.sleep(1);
        driver.executeScript("arguments[0].click();", submit);

        TimeUnit.SECONDS.sleep(5);

        getNext(driver, 1);
    }

    static class Job {
        private String job;
        private String name;
        private String salary;
        private String city;
        private String edu;

        public String getJob() {
            return job;
        }

        public void setJob(String job) {
            this.job = job;
        }

        public String getName() {
            return name;
        }

        public void setName(String name) {
            this.name = name;
        }

        public String getSalary() {
            return salary;
        }

        public void setSalary(String salary) {
            this.salary = salary;
        }

        public String getCity() {
            return city;
        }

        public void setCity(String city) {
            this.city = city;
        }

        public String getEdu() {
            return edu;
        }

        public void setEdu(String edu) {
            this.edu = edu;
        }
    }
}
